home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 30.1 KB | 1,183 lines | [TEXT/KAHL] |
- /********************************************************* DEFINITION
- DATE: 9/19/93
- AUTHOR: Eric R. Rosé
-
- CLASS: CPPText
-
- SUPERCLASS: CPPObject
-
- This C++ class manages a textedit area, and a horizontal and
- vertical scrollbar which let you adjust the text.
-
- ********************************************************************/
-
- #include <stdlib.h>
- #include <CPPText.h>
- #include <CPPWindow.h>
- #include <ctype.h>
- #include <Commands.h>
- #include <mathTools.h>
-
- extern Rect kEmptyRect;
-
- /*-----------------------------------------------------------------*/
- /*---------------------- CALLBACK VARIABLES -----------------------*/
- /*-----------------------------------------------------------------*/
-
- // Initialize the class member variables used in the callbacks
- CPPText *CPPText::gCurrentTE = NULL;
- ControlHandle CPPText::gVScroll = NULL;
- ControlHandle CPPText::gHScroll = NULL;
- TEHandle CPPText::gTextBlock = NULL;
-
- /*-----------------------------------------------------------------*/
- /*------------------------ PUBLIC METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- CPPText::CPPText (CPPWindow *OurWindow,
- Rect *ViewArea,
- Rect *DestArea,
- short MaxLength,
- Boolean UseHScroll,
- Boolean UseVScroll,
- short Font, short FSize) :
- CPPVisualObject (OurWindow, ViewArea, TRUE)
- {
- MakeTEArea (OurWindow, ViewArea, DestArea, MaxLength,
- UseHScroll, UseVScroll, Font, FSize);
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPText::CPPText (CPPWindow *OurWindow,
- short MaxLength,
- Boolean UseHScroll,
- Boolean UseVScroll,
- short Font, short FSize) :
- CPPVisualObject (OurWindow,
- &((OurWindow->GetWindow())->portRect),
- TRUE)
- {
- Rect ViewRect;
- Rect DestRect;
-
- if (OurWindow)
- {
- ViewRect = (OurWindow->GetWindow())->portRect;
- SetRect (&DestRect, 0, 0, kPageWidth, kPageHeight);
- MakeTEArea(OurWindow, &ViewRect, &DestRect, MaxLength,
- UseHScroll, UseVScroll, Font, FSize);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPText::~CPPText (void)
- /* Dispose of the controls and TEHandle used by the object */
- {
- if (this->TextBlock)
- TEDispose(this->TextBlock);
- if (this->HScroll)
- DisposeControl(this->HScroll);
- if (this->VScroll)
- DisposeControl(this->VScroll);
- }
-
- /*-----------------------------------------------------------------*/
-
- char *CPPText::ClassName (void)
- {
- return "CPPText";
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPText::DoCommand (short commandID)
- /* do a command associated with a menu */
- {
- switch (commandID) {
- case kCmdCut :
- DoCut();
- break;
- case kCmdCopy :
- DoCopy();
- break;
- case kCmdPaste :
- DoPaste();
- break;
- case kCmdSelectAll :
- DoSelectAll();
- break;
- case kCmdClear :
- if (this->TextBlock)
- TEDelete(this->TextBlock);
- break;
- default:
- return FALSE;
- break;
- }
-
- return TRUE;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::SetMinSize (short mwidth, short mheight)
- {
- this->TEminWidth = mwidth;
- this->TEminHeight = mheight;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::SetMaxSize (short mwidth, short mheight)
- {
- this->TEmaxWidth = mwidth;
- this->TEmaxHeight = mheight;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::Activate (Boolean nowActive)
- /* activate or deactivate the text depending on DoActivate */
- /* Is Override */
- {
- short HiliteValue = (nowActive) ? 0 : 255;
-
- CPPVisualObject::Activate(nowActive);
-
- if (this->TextBlock)
- {
- if (nowActive)
- TEActivate(this->TextBlock);
- else
- TEDeactivate (this->TextBlock);
- AdjustScrollBar();
-
- if (this->VScroll)
- HiliteControl (this->VScroll, HiliteValue);
- if (this->HScroll)
- HiliteControl (this->HScroll, HiliteValue);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoIdle (void)
- /* Is Override */
- {
- if (this->IsActive() && this->TextBlock)
- TEIdle(this->TextBlock);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::TypeChar (unsigned char TheKey)
- /* type a character into the TE area at the current insertion point */
- {
- Boolean canType = TRUE;
- short SelectionLen = 0;
-
- if (this->TextBlock)
- {
- SelectionLen = (*this->TextBlock)->selEnd -
- (*this->TextBlock)->selStart;
- if (isprint(TheKey))
- canType = (*this->TextBlock)->teLength - SelectionLen <
- this->maxTextLen;
- if (canType)
- {
- TEKey(TheKey, this->TextBlock);
- AdjustScrollBar();
- CheckInsertion();
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoCut (void)
- /* cut the current selection of the TE area */
- {
- long dummy;
-
- if (this->TextBlock)
- {
- CheckInsertion();
- TECut (this->TextBlock);
- AdjustScrollBar();
- CheckInsertion();
- dummy = ZeroScrap();
- dummy = TEToScrap();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoCopy (void)
- /* copy the current selection of the TE area */
- {
- long dummy;
-
- if (this->TextBlock)
- {
- AdjustScrollBar();
- TECopy (this->TextBlock);
- dummy = ZeroScrap();
- dummy = TEToScrap();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoPaste (void)
- /* paste the contents of the scrap into the TE area */
- {
- long dummy;
- short SelectionLen;
-
- if (this->TextBlock)
- {
- SelectionLen = (*this->TextBlock)->selEnd -
- (*this->TextBlock)->selStart;
- if (TEFromScrap() != noErr)
- SysBeep(5);
- else
- {
- CheckInsertion();
- if ((TEGetScrapLen() +
- (*this->TextBlock)->teLength -
- SelectionLen) <= this->maxTextLen)
- { // if the selection will fit, paste it in
- TEPaste (this->TextBlock);
- AdjustScrollBar();
- CheckInsertion();
- }
- else // change the scrap size and paste
- {
- TESetScrapLen(this->maxTextLen -
- (*this->TextBlock)->teLength +
- SelectionLen);
- TEPaste (this->TextBlock);
- AdjustScrollBar();
- CheckInsertion();
- }
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoClear (void)
- /* Clear the current selection in the TE area */
- {
- long dummy;
-
- if (this->TextBlock)
- {
- CheckInsertion();
- TEDelete (this->TextBlock);
- AdjustScrollBar();
- CheckInsertion();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoSelectAll (void)
- /* select all of the text in the TE area */
- {
- if (this->TextBlock)
- {
- CheckInsertion();
- TESetSelect(0, 32000, this->TextBlock);
- AdjustScrollBar();
- CheckInsertion();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- short CPPText::GetTextLen (void)
- {
- if (this->TextBlock)
- return (**this->TextBlock).teLength;
- else
- return 0;
- }
-
- /*-----------------------------------------------------------------*/
-
- CharsHandle CPPText::GetTheText (void)
- /* return a handle to the text in the TE object */
- {
- if (this->TextBlock)
- return TEGetText (this->TextBlock);
- else
- return NULL;
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPText::DoKey (char theKey, short modifiers, short what)
- {
- if (!IsVisible()) return FALSE;
-
- if (modifiers & cmdKey)
- { /* Command key down. */
- if (what == keyDown)
- { // handle default edit commands
- switch (theKey) {
- case 'x' :
- case 'X' : DoCut(); break;
- case 'c' :
- case 'C' : DoCopy(); break;
- case 'v' :
- case 'V' : DoPaste(); break;
- case 'a' :
- case 'A' : DoSelectAll(); break;
- default : return FALSE; // unknown command
- break;
- }
- return TRUE; // we handled the cut/copy/paste
- }
- }
- else
- TypeChar (theKey);
- }
-
- /*-----------------------------------------------------------------*/
-
- Boolean CPPText::DoClick (EventRecord *theEvent)
- /* Is Override */
- {
- WindowPtr theWindow;
- short code, part;
- ControlHandle hitControl;
- Point myPt = theEvent->where;
- TEPtr TE;
- Boolean shiftDown = (theEvent->modifiers & shiftKey) ? TRUE : FALSE;
-
- if (!IsVisible()) return FALSE;
-
- code = FindWindow(theEvent->where, &theWindow);
- if (theWindow == this->owningWindow)
- {
- Global2Local (&myPt);
- part = FindControl(myPt, theWindow, &hitControl);
-
- // now respond to the click
- if (hitControl) // if they clicked in a scrollbar
- {
- if (hitControl == this->HScroll)
- DoHScroller (myPt, part);
- else
- if (hitControl == this->VScroll)
- DoVScroller (myPt, part);
- else
- return FALSE;
- }
- else // if they clicked in the TE area
- {
- if (this->TextBlock)
- {
- TE = *this->TextBlock;
- if (PtInRect(myPt, &TE->viewRect))
- {
- gVScroll = this->VScroll;
- gHScroll = this->HScroll;
- gTextBlock = this->TextBlock;
- gCurrentTE = this;
- TEClick (myPt, shiftDown, this->TextBlock);
- }
- else
- return FALSE;
- }
- else
- return FALSE;
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::InsertTextPtr (Ptr theText, long textLen,
- long insertWhere,
- Boolean ScrollToInsertion)
- /* insert the text pointed to by TheText into the TE area */
- /* at the specified location */
- {
- short maxAllowed; // use to keep within length limits
-
- if (this->TextBlock && theText)
- {
- maxAllowed = this->maxTextLen - (*this->TextBlock)->teLength;
-
- // let us watch what's going on
- if (ScrollToInsertion)
- CheckInsertion();
-
- // set the selection and insert
- TESetSelect(insertWhere, insertWhere, this->TextBlock);
- TEInsert(theText, Min(maxAllowed, textLen), this->TextBlock);
-
- // adjust for the text we inserted
- if (ScrollToInsertion)
- CheckInsertion();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::InsertTextHandle (Handle theText, long textLen,
- long where,
- Boolean ScrollToInsertion)
- /* insert the text pointed to by TheText into the TE area */
- /* in place of/at the current selection */
- {
- Boolean wasLocked;
-
- if (this->TextBlock && theText)
- {
- wasLocked = (HGetState(theText) & 0x0080) ? TRUE : FALSE;
- HLock(theText);
-
- InsertTextPtr (*theText, textLen, where, ScrollToInsertion);
-
- if (!wasLocked)
- HUnlock(theText);
- }
-
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::SetTextPtr (Ptr theText, long textLen,
- Boolean ScrollToInsertion)
- /* set the TE area to contain only the text pointed to by TheText */
- {
- DoSelectAll(); // select all the text in the area
- DoClear(); // delete it all
- InsertTextPtr(theText, Min(textLen, this->maxTextLen),
- 1, ScrollToInsertion);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::SetTextHandle (Handle theText, long textLen,
- Boolean ScrollToInsertion)
- /* set the TE area to contain only the text pointed to by TheText */
- {
- DoSelectAll(); // select all the text in the area
- DoClear(); // delete it all
- InsertTextHandle(theText, Min(textLen, this->maxTextLen),
- 1, ScrollToInsertion);
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::MakeVisible (Boolean nowVisible)
- /* Hide or show the text edit area, depending on 'nowVisible' */
- {
- Rect boundsRect = *GetBounds();
-
- InsetRect (&boundsRect, -1, -1);
- // exit if it is already in the desired state
- if (nowVisible == IsVisible())
- return;
-
- CPPVisualObject::MakeVisible (nowVisible);
-
- // if it is currently invisible, erase it;
- // in either case, invalidate the area to force a redraw
- if (!IsVisible())
- {
- EraseRect (&boundsRect);
- if (this->VScroll)
- HideControl(VScroll);
- if (this->HScroll)
- HideControl(HScroll);
- }
- else
- {
- if (this->VScroll)
- ShowControl(VScroll);
- if (this->HScroll)
- ShowControl(HScroll);
- this->Draw();
- }
-
- InvalRect(&boundsRect);
-
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::Draw (void)
- /* draw the text edit area and its controls */
- {
- Rect tempRect;
- if (this->owningWindow && this->TextBlock && this->IsVisible())
- {
- tempRect = (*this->TextBlock)->viewRect;
- InsetRect (&tempRect, -1, -1);
- FrameRect(&tempRect);
- TEUpdate (&this->owningWindow->portRect, this->TextBlock);
- if (this->VScroll)
- Draw1Control (this->VScroll);
- if (this->HScroll)
- Draw1Control (this->HScroll);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::TargetHilite (Boolean makeTarget)
- /* change the hiliteed state of the object (some objects, like */
- /* lists, have different appearances when they are the target) */
- /* SUBCLASS SHOULD OVERRIDE */
- {
- this->Activate(makeTarget);
-
- CPPVisualObject::TargetHilite(makeTarget);
- }
-
- /*-----------------------------------------------------------------*/
-
- Rect *CPPText::GetBounds (void)
- /* return the boundsrect of the object */
- /* SUBCLASS MUST OVERRIDE */
- {
- SetRect (&this->bounds, 0, 0, 0, 0);
-
- if (this->isVisible)
- {
- if (this->TextBlock)
- this->bounds = (*this->TextBlock)->viewRect;
- if (this->VScroll)
- UnionRect (&this->bounds, &((*this->VScroll)->contrlRect), &this->bounds);
- if (this->HScroll)
- UnionRect (&this->bounds, &((*this->HScroll)->contrlRect), &this->bounds);
- }
- else
- this->bounds = kEmptyRect;
-
- return &this->bounds;
- }
-
-
- /*-----------------------------------------------------------------*/
- /*---------------------- PROTECTED METHODS ------------------------*/
- /*-----------------------------------------------------------------*/
-
- void CPPText::MoveContent (short newH, short newV)
- /* move the entire text edit area to the new position */
- {
- Rect vRect, dRect, bRect = *GetBounds();
- short deltaH = newH - bRect.left,
- deltaV = newV - bRect.top;
-
- // move the text block
- if (this->TextBlock)
- {
- vRect = (*this->TextBlock)->viewRect;
- dRect = (*this->TextBlock)->destRect;
- OffsetRect (&((*this->TextBlock)->viewRect), newH-dRect.left,
- newV-dRect.top);
- OffsetRect (&((*this->TextBlock)->destRect), newH-dRect.left,
- newV-dRect.top);
- }
- // move the vertical scroll bar
- if (this->VScroll)
- {
- vRect = (*this->VScroll)->contrlRect;
- MoveControl (this->VScroll, vRect.left + deltaH,
- vRect.top + deltaV);
- }
- // move the horizontal scroll bar
- if (this->HScroll)
- {
- dRect = (*this->HScroll)->contrlRect;
- MoveControl (this->HScroll, dRect.left + deltaH,
- dRect.top + deltaV);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::ResizeContent (short newWidth, short newHeight)
- {
-
- TEPtr TE = (this->TextBlock) ? *this->TextBlock : NULL;
- Rect VCRect, HCRect, GrowRect, ViewRect, DestRect;
- Rect PRect;
-
- if (this->owningWindow && this->TextBlock)
- {
- // figure out what the new size of the view area is
- ViewRect = TE->viewRect;
- ViewRect.right = Min (ViewRect.left + newWidth, this->TEmaxWidth)
- - ((this->VScroll) ? 15 : 0);
- ViewRect.bottom = Min (ViewRect.top + newHeight, this->TEmaxHeight)
- - ((this->HScroll) ? 15 : 0);
-
- // move and resize the controls
- if (this->HScroll)
- {
- HideControl (this->HScroll);
- MoveControl (this->HScroll, ViewRect.left-1, ViewRect.bottom);
- SizeControl (this->HScroll, (ViewRect.right - ViewRect.left) + 2, 16);
- ShowControl (this->HScroll);
- }
-
- if (this->VScroll)
- {
- HideControl (this->VScroll);
- MoveControl (this->VScroll, ViewRect.right, ViewRect.top-1);
- SizeControl (this->VScroll, 16, (ViewRect.bottom - ViewRect.top) + 2);
- ShowControl (this->VScroll);
- }
-
- // resize the text edit area
- TE->viewRect = ViewRect;
-
- // adjust the positions of everything
- AdjustScrollBar();
- CheckInsertion();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::CheckInsertion (void)
- /* this procedure checks the current insertion point and adjusts */
- /* the text in the window so that the insertion point is always */
- /* visible */
- {
- short topLine, bottomLine, windowLines;
- TEPtr TE;
-
- if (this->VScroll && this->TextBlock)
- {
- TE = *this->TextBlock;
- windowLines = LinesInWindow();
- topLine = GetCtlValue (this->VScroll);
- bottomLine = topLine + windowLines;
-
- if (GetCtlMax (this->VScroll) == 0)
- MoveScrollText();
- else
- if (TE->selEnd < TE->lineStarts[topLine])
- ScrollChar (TE->selStart, FALSE);
- else
- if (TE->selStart >= TE->lineStarts[bottomLine])
- ScrollChar (TE->selEnd, TRUE);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::ScrollChar (short charPos, Boolean toBottom)
- /* Adjust the scroll bar to match the current position of the */
- /* text after text has been inserted or deleted */
- {
- short theLine = 0;
- TEPtr TE;
-
- if (this->TextBlock && this->VScroll)
- {
- TE = *this->TextBlock;
- while (TE->lineStarts[theLine+1] <= charPos)
- theLine++;
- if (toBottom)
- theLine = theLine - (LinesInWindow() - 1);
- SetCtlValue(this->VScroll, theLine);
- MoveScrollText();
- }
- }
-
- /*-----------------------------------------------------------------*/
- /*----------------------- PRIVATE METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- long CPPText::LinesInWindow(void)
- {
- TEPtr TE = (this->TextBlock) ? *this->TextBlock : NULL;
-
- if (TE)
- return (TE->viewRect.bottom - TE->viewRect.top) /
- TE->lineHeight;
- else
- return 0;
- }
-
- /*-----------------------------------------------------------------*/
-
- long CPPText::LinesInText (void)
- {
- long Lines;
- short Len;
- TEPtr TE;
-
- if (this->TextBlock)
- {
- TE = *this->TextBlock;
- Len = TE->teLength;
-
- Lines = TE->nLines;
- if (Len > 0)
- if (*(*TE->hText + Len-1) == 13)
- Lines += 1;
- return Lines;
- }
- else
- return 0;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::MoveScrollText ()
- /* Scroll the text to match the settings of the scrollbars */
- {
- long ScrollValue,
- height, i, j,
- HScrollDiff = 0, VScrollDiff = 0,
- OldScroll, NewScroll;
- TEPtr TE;
-
- if (this->TextBlock)
- {
- TE = *this->TextBlock;
- if (this->VScroll)
- {
- OldScroll = TE->viewRect.top - TE->destRect.top;
- ScrollValue = GetCtlValue (this->VScroll);
- NewScroll = ScrollValue * TE->lineHeight;
- VScrollDiff = OldScroll - NewScroll;
- }
-
- if (this->HScroll)
- {
- OldScroll = TE->viewRect.left - TE->destRect.left;
- ScrollValue = GetCtlValue (this->HScroll);
- NewScroll = ScrollValue * TE->lineHeight;
- HScrollDiff = OldScroll - NewScroll;
- }
-
- if (abs(VScrollDiff) > 32000)
- { // if we have reached the limit of textedit, scroll & beep
- TEScroll (HScrollDiff, SIGN(VScrollDiff) * 32000, this->TextBlock);
- SysBeep(2);
- }
- else // otherwise, do the scrolling, if any is required
- if (VScrollDiff + HScrollDiff != 0)
- TEScroll (HScrollDiff, VScrollDiff, this->TextBlock);
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- pascal void CPPText::Scroll_Up (ControlHandle theControl, short ctlPart)
- /* this method serves as a callback routine for TrackControl; */
- /* It forces the text to scroll up 1 line */
- {
- if (theControl && (GetCtlValue(theControl) - 1 >= GetCtlMin(theControl)))
- {
- SetCtlValue (theControl, GetCtlValue (theControl)-1);
- gCurrentTE->MoveScrollText();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- pascal void CPPText::Scroll_Down (ControlHandle theControl,
- short ctlPart)
- /* this method serves as a callback routine for TrackControl; */
- /* It forces the text to scroll down 1 line */
- {
- if (theControl && (GetCtlValue(theControl) + 1 <= GetCtlMax(theControl)))
- {
- SetCtlValue (theControl, GetCtlValue (theControl)+1);
- gCurrentTE->MoveScrollText();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- pascal void CPPText::Scroll_Left (ControlHandle theControl,
- short ctlPart)
- /* this method serves as a callback routine for TrackControl; */
- /* It forces the text to scroll left 1 line */
- {
- if (theControl && (GetCtlValue(theControl) - 1 >= GetCtlMin(theControl)))
- {
- SetCtlValue (theControl, GetCtlValue (theControl)-1);
- gCurrentTE->MoveScrollText();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- pascal void CPPText::Scroll_Right (ControlHandle theControl,
- short ctlPart)
- /* this method serves as a callback routine for TrackControl; */
- /* It forces the text to scroll right 1 line */
- {
- if (theControl && (GetCtlValue(theControl) + 1 <= GetCtlMax(theControl)))
- {
- SetCtlValue (theControl, GetCtlValue (theControl)+1);
- gCurrentTE->MoveScrollText();
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- pascal Boolean CPPText::AutoScroll (void)
- {
- Boolean result = TRUE;
- RgnHandle oldClip = NewRgn();
- Point mouseLoc;
- Rect textRect, tempRect;
-
- GetClip (oldClip);
- tempRect = (*gTextBlock)->viewRect;
- if (gVScroll)
- UnionRect (&tempRect, &(*gVScroll)->contrlRect, &tempRect);
- if (gHScroll)
- UnionRect (&tempRect, &(*gHScroll)->contrlRect, &tempRect);
- ClipRect(&tempRect);
-
- GetMouse (&mouseLoc);
- textRect = (*gTextBlock)->viewRect;
- if (mouseLoc.v < textRect.top)
- Scroll_Up (gVScroll, inUpButton);
- else
- if (mouseLoc.v > textRect.bottom)
- Scroll_Down (gVScroll, inDownButton);
-
- if (mouseLoc.h > textRect.right)
- Scroll_Right (gHScroll, inDownButton);
- else
- if (mouseLoc.h < textRect.left)
- Scroll_Left (gHScroll, inUpButton);
-
- SetClip (oldClip);
- DisposeRgn(oldClip);
- return TRUE;
-
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::VPageScroll (long part, long direction)
- /* Scroll the text 1 page either up or down */
- {
- Point tempPt;
- short newValue, page;
- TEPtr TE;
-
- if (this->VScroll && this->TextBlock)
- {
- TE = *this->TextBlock;
- GetMouse (&tempPt);
- while (StillDown())
- {
- if (TestControl(this->VScroll, tempPt) == part)
- {
- page = direction * ((TE->viewRect.top -
- TE->viewRect.bottom) /
- (TE->lineHeight - 1));
- newValue = GetCtlValue (this->VScroll) + page;
- SetCtlValue (this->VScroll, newValue);
- MoveScrollText();
- }
- GetMouse (&tempPt);
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::HPageScroll (long part, long direction)
- /* Scroll the text 1 page either left or right */
- {
- Point tempPt;
- short newValue, page;
- TEPtr TE;
-
- if (this->HScroll && this->TextBlock)
- {
- TE = *this->TextBlock;
- GetMouse (&tempPt);
- while (StillDown())
- {
- if (TestControl(this->HScroll, tempPt) == part)
- {
- page = direction * ((TE->viewRect.right -
- TE->viewRect.left) /
- (TE->lineHeight - 1));
- newValue = GetCtlValue (this->HScroll) + page;
- SetCtlValue (this->HScroll, newValue);
- MoveScrollText();
- }
- GetMouse (&tempPt);
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::AdjustScrollBar (void)
- /* This procedure turns the scroll bar off if there are fewer */
- /* lines than space for lines, and turns it on to its maximum */
- /* position if there are more */
- {
- short windowLines,
- currentLines,
- currentWidth;
- TEPtr TE;
-
- if (this->TextBlock)
- {
- TE = *this->TextBlock;
-
- if (this->VScroll)
- {
- windowLines = LinesInWindow();
- currentLines = LinesInText();
- if (currentLines > windowLines)
- SetCtlMax (this->VScroll, currentLines - windowLines);
- else
- {
- SetCtlMax (this->VScroll, 0);
- SetCtlMin (this->VScroll, 0);
- }
- }
-
- if (this->HScroll)
- {
- currentWidth = abs((TE->viewRect.right -
- TE->destRect.right) / TE->lineHeight);
- if (currentWidth)
- SetCtlMax (this->HScroll, currentWidth);
- else
- {
- SetCtlMax (this->HScroll, 0);
- SetCtlMin (this->HScroll, 0);
- }
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
- //
- // Boolean CPPText::AutoScroll (void)
- /* this is the function which is called if you click in the */
- /* TextEdit field and drag the mouse down or up beyond the */
- /* bottom or top of the window. It should be set with the */
- /* SetClikLoop function for the TextEdit field you are using */
- /* {
- RgnHandle oldClip;
- Point mouseLoc;
- Rect textRect;
- Rect tempRect;
- TEPtr TE;
-
- if (!this->TextBlock)
- return FALSE;
- TE = *this->TextBlock;
-
- oldClip = NewRgn();
- GetClip (oldClip);
-
- tempRect = TE->viewRect;
- if (this->VScroll)
- UnionRect (&tempRect, &((*this->VScroll)->contrlRect), &tempRect);
- if (this->HScroll)
- UnionRect (&tempRect, &((*this->HScroll)->contrlRect), &tempRect);
- ClipRect (&tempRect);
- GetMouse(&mouseLoc);
- textRect= TE->viewRect;
- if (mouseLoc.v < textRect.top)
- Scroll_Up(this->VScroll, inUpButton);
- else
- if (mouseLoc.v > textRect.bottom)
- Scroll_Down (this->VScroll, inDownButton);
-
- if (mouseLoc.h > textRect.right)
- Scroll_Right (this->HScroll, inDownButton);
- else
- if (mouseLoc.h < textRect.left)
- Scroll_Left (this->HScroll, inUpButton);
-
- SetClip (oldClip);
- DisposeRgn(oldClip);
-
- return TRUE;
- }
- */
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoVScroller (Point clickPt, short part)
- /* Handle a click in the vertical scroll bar */
- {
- long Result;
-
- gCurrentTE = this;
-
- if (this->VScroll)
- {
- switch (part) {
- case inUpButton :
- Result = TrackControl(this->VScroll, clickPt,
- (ProcPtr)Scroll_Up);
- break;
- case inDownButton :
- Result = TrackControl(this->VScroll, clickPt,
- (ProcPtr)Scroll_Down);
- break;
- case inPageUp :
- VPageScroll (part, 1);
- break;
- case inPageDown :
- VPageScroll (part, -1);
- break;
- case inThumb :
- TrackControl (this->VScroll, clickPt, NULL);
- MoveScrollText();
- break;
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::DoHScroller (Point clickPt, short part)
- /* Handle a click in the horizontal scroll bar */
- {
- long Result;
-
- gCurrentTE = this;
-
- if (this->HScroll)
- {
- switch (part) {
- case inUpButton :
- Result = TrackControl(this->HScroll, clickPt,
- (ProcPtr)Scroll_Left);
- break;
- case inDownButton :
- Result = TrackControl(this->HScroll, clickPt,
- (ProcPtr)Scroll_Right);
- break;
- case inPageUp :
- HPageScroll (part, -1);
- break;
- case inPageDown :
- HPageScroll (part, 1);
- break;
- case inThumb :
- TrackControl (this->HScroll, clickPt, NULL);
- MoveScrollText();
- break;
- }
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPText::MakeTEArea (CPPWindow *OurWindow,
- Rect *ViewArea,
- Rect *DestArea,
- short maxLength,
- Boolean UseHScroll,
- Boolean UseVScroll,
- short Font, short FSize)
- {
- Rect DestRect = *DestArea;
- Rect ViewRect = *ViewArea;
- Rect TempRect;
- GrafPtr savePort;
- short OldFont, OldFace, OldSize;
-
- if (!OurWindow)
- return;
- this->owningWindow = OurWindow->GetWindow();
- this->maxTextLen = maxLength;
-
- GetPort(&savePort);
- SetPort (this->owningWindow);
-
- // save the font, size and style of the current grafport
- OldFont = this->owningWindow->txFont;
- OldSize = this->owningWindow->txSize;
-
- // set the font so that the TE area will appear correctly
- TextFont (Font);
- TextSize (FSize);
-
- // adjust the size of the view rectangle if there are scrollbars
- if (UseHScroll)
- ViewRect.bottom -= (kSBarWidth - 1);
- if (UseVScroll)
- ViewRect.right -= (kSBarWidth - 1);
-
- this->TextBlock = TENew (&DestRect, &ViewRect);
- if (!this->TextBlock)
- {
- SetPort (savePort);
- return;
- }
-
- if (UseVScroll)
- {
- SetRect (&TempRect, ViewRect.right, ViewRect.top-1,
- ViewRect.right+16, ViewRect.bottom+1);
- this->VScroll = NewControl(this->owningWindow, &TempRect, "\pVScroller",
- TRUE, 0, 0, 0, scrollBarProc, 1101);
- if (!this->VScroll)
- {
- TEDispose(this->TextBlock);
- SetPort (savePort);
- return;
- }
- }
- else
- this->VScroll = NULL;
-
- if (UseHScroll)
- {
- SetRect (&TempRect, ViewRect.left-1, ViewRect.bottom,
- ViewRect.right+1, ViewRect.bottom + 16);
- this->HScroll = NewControl(this->owningWindow, &TempRect, "\pHScroller",
- TRUE, 0, 0, 0, scrollBarProc, 1102);
- if (!this->HScroll)
- {
- TEDispose(this->TextBlock);
- if (this->VScroll)
- DisposeControl(this->VScroll);
- SetPort (savePort);
- return;
- }
- }
- else
- this->HScroll = NULL;
-
- SetMinSize (70, 70);
- SetMaxSize ((DestArea->right - DestArea->left) + 16,
- (DestArea->bottom - DestArea->top) + 16);
-
- SetClikLoop (AutoScroll, this->TextBlock);
-
- // restore the original font and size
- TextSize (OldSize);
- TextFont (OldFont);
-
- // restore the saved port
- SetPort (savePort);
- }
-
- /*-----------------------------------------------------------------*/
-
-
-